I want to set up a simple queue service that can take several messages created rapidly on the server
side and display them individually on the client side.
For instance, when we enter the game, we may trigger three or four events, but we want to show the
messages in a way that the user can actually read it.
We don't want to just spam them with a bunch of windows that pop up.
So I'm going to create this simple queue service in its own game and I'm going to move it to my zombie
game.
Let's take a look at what we got.
Hey, I hit the play button.
All of a sudden, we're going to get three messages that are just created instantly right there.
And now you can see that the player has time to read them.
And we could slow this down if we wanted to.
I thought this would be really useful.
I created an empty base plate for my Q service.
There's nothing in it.
I'm going to start off by going to replicate, add storage, hit the plus, add a remote event because
we're going to be talking from server to client.
I'm going to call the remote event info message r E, and we're going to need something to generate
messages for our test.
Let's go to server script service, hit the plus, add a script and I'll call this message gen.
There we go.
Let's get rid of our print statement and get a variable for replicated storage.
Call it RS game, get service, replicated storage, and then in replicated storage, I'm going to get
my info message remote event or info message re.
It is in replicated storage.
We'll do a wait for child info message r a now type in game players to get the player service.
We'll get the player added event from the player service.
Connect that to an anonymous function with a player gets passed in.
We could do a print statement just so we know that we've we haven't made any mistakes up to this point.
We'll do player added and then perhaps wait 5 seconds just so that you you can you can view stuff in
the world before things start firing off.
You get in there All right.
And then I'm going to do a prep statement.
I'm going to say print sending messages.
So I know that if I'm waiting too long after that, something has failed.
If I'm waiting at all after that, something has failed.
I'm going to make a for loop, for I equals 1 to 3.
We'll do three messages in steps of one.
Do make a variable for your message.
And what I say I call this something like this is message number.
Put a space speech quote dot, dot I.
So we're going to concatenate the number on our string with these two dots.
Now, what should I do?
Let's print it out.
Print.
Server side message and let's do MSG.
What else we will fire.
Let's fire our remote event.
So we're going to fire the remote event.
Nothing's there to catch it yet, but we'll go ahead and fire it.
Fire client.
You're going to need the player to pass it to.
And the message.
All right, let's try this out.
We should only be able to see stuff on the server side now because we didn't do anything with the client
side.
I'll hit the play button.
We'll go to view output.
So I get my output window view output.
There it is.
And there's our messages.
So we had our player added, sending messages.
We've got three messages.
Let's go ahead and turn off our game and work on the client side stuff now.
So I'll just turn this off.
And I'm going to close this window right here.
Let's go to our starter guy, because we're going to do this stuff and we're going to display messages
with the with the guy.
So let's hit the plus sign screen gooey and then on the screen gooey.
I'm going to do a local script and the local script, I'm going to call this process
MSG script.
On the client side, we are also going to have to get a variable for our info message.
RE That's the gateway.
So we have to connect on both sides.
Let's get a variable for replicated storage game, get service, replicated storage, and then I'm going
to get a variable for info, what I call that info message.
Ari I probably should just copied it from the other one or a replicated storage wait for child info
message.
Ari.
I'm also going to get a variable for my message queue.
It's basically just going to be a table, but a queue is something where when you put messages in it,
the first one out is going to be the first one in.
That's why they call it a queue as opposed to a stack.
Like if you stack something, the last thing on the stack is going to be the first thing you pull off.
Or they say Pop, they pop, you pop something off the stack, but the queue, the first one in is going
to be the first one out.
Now I'm going to put messages in my message queue so we have our info message.
R e that's firing messages off.
We can capture them by doing our message info r e on client event connecting that to an anonymous function.
If you remember on the other side, we passed in the player and the message the player is used to get
to you.
Right?
This is a local script running locally on your computer, so we don't have the player here.
We're just going to get the MSG the message.
Then I'll use this table insert to insert my message into this table.
And here's the message We didn't provide an index, so it's just going to put the message on the end.
So this queue is going to continue to get messages.
We need to process the messages or else it doesn't really pay to have a queue.
So let's do a function local function process, MSG, and then we're going to do a while loop and I'll
just put a weight in here.
So we'll pull every 2 seconds, maybe every one second.
We don't want to wait that long.
And so while wait one second, do let's check for if there are messages in our queue, I'm going to
look for the number of messages in our MSG queue check to see if it's greater than zero.
If it is, I'm going to process a message and right now our processing just means printing it.
So I'll do a client side process.
MSG queue whoops, MSG queue bracket one.
We're going to get the first message in the queue.
Remember first in, first out, then we're going to remove we'll do a table, remove MSG, queue that
first, that first message right there.
And then the other condition that can happen is if the queue is empty.
So if there's nothing in the queue.
We'll do a print.
The queue is empty and we need to call this, but we don't want it to interrupt anything.
It's got an infinite while loop, so we have to be careful of that.
Let's go down to the bottom and I think I'm just going to spawn process message in its own thread.
So we're just going to spawn this off and then it's going to start running.
It's not going to interfere with this script right here.
All right, let's try it out and hit the play button.
I'm going to go to my view tab and hit the output.
Q is empty and boom, we got some messages and you can see that they printed out one a second after
that and then the Q is empty again.
That's pretty cool.
Let's add a UI and then we can actually start displaying the message to the user in a way that would
make sense for a player in a game.